package com.aivin.maproutelib.tool;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.text.TextUtils;
import java.util.HashMap;
import java.util.Map;


public class BitmapWrap {
    private Map<Integer, Bitmap> map = new HashMap<>();
    private static final int TRANSPARENT_BITMAP_ID = 0x125;
    // 字体大小，为了适配不同机型
    private final static int TEXT_SIZE_PX_L = 40;
    private final static int TEXT_SIZE_PX_M = 24;
    private final static int TEXT_SIZE_PX_S = 15;
    private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);

    public BitmapWrap() {
        paint.setColor(Color.BLACK);
        paint.setTextAlign(Paint.Align.CENTER);

    }

    public Bitmap getBitmap(Context context ,int resId, String text) {
        Bitmap bitmap = map.get(resId);
        if (bitmap == null) {
            if (resId == TRANSPARENT_BITMAP_ID) {
                bitmap = Bitmap.createBitmap(40, 40, Bitmap.Config.ARGB_8888);
            } else {
                bitmap = BitmapFactory.decodeResource( context.getResources(), resId);
            }
            map.put(resId, bitmap);
        }

        if (resId == TRANSPARENT_BITMAP_ID) {
            paint.setTextSize(TEXT_SIZE_PX_L);
        } else {
            paint.setTextSize(TEXT_SIZE_PX_S);
        }

        Bitmap copy = bitmap.copy(Bitmap.Config.ARGB_8888, true);
        Canvas canvas = new Canvas(copy);

        Rect clipBounds = canvas.getClipBounds();
        Rect rect = new Rect(clipBounds.left, clipBounds.top + 1, clipBounds.right, clipBounds.bottom - 1);
        canvas.clipRect(rect);

        if (!TextUtils.isEmpty(text)) {
            String[] split = text.split("&");
            int length = split.length;

            if (length == 1) {
                Paint.FontMetrics fontMetrics = paint.getFontMetrics();
                float v = fontMetrics.bottom - fontMetrics.top;
                int height = (-rect.top + rect.bottom);
                float moreHei = height - v;
                float baseLine = height - moreHei / 2 - fontMetrics.bottom;
                canvas.drawText(text, copy.getWidth() / 2, baseLine, paint);
            } else if (length == 2) {
                paint.setTextSize(TEXT_SIZE_PX_M);
                Paint.FontMetrics fontMetrics = paint.getFontMetrics();
                float v = fontMetrics.bottom - fontMetrics.top;
                int height = (-rect.top + rect.bottom) / 2;
                float moreHei = height - v;
                float baseLine = height - moreHei / 2 - fontMetrics.bottom;
                paint.setColor(Color.RED);
                canvas.drawText(split[0], copy.getWidth() / 2, baseLine - moreHei / 2, paint);
                canvas.drawText(split[1], copy.getWidth() / 2, height + baseLine + moreHei / 2, paint);
            }
        }

        return copy;
    }




    public Bitmap getBitmapWidthStr(String text) {
        Bitmap bitmap = Bitmap.createBitmap(80, 80, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        Rect rectF = canvas.getClipBounds();

        //paint.setColor(Color.GRAY);
        //canvas.drawRect(rectF ,paint);
        Paint.FontMetrics fontMetrics= paint.getFontMetrics();
        float distance0=(fontMetrics.bottom - fontMetrics.top)/2 - fontMetrics.bottom;
        float baseY=rectF.centerY() + distance0; // 基点坐标Y = 矩形Y坐标 + 矩形中心与基线的距离
        float baseX = bitmap.getWidth()/2f ;
        paint.setColor(Color.RED);
        paint.setTextSize(30);
        canvas.drawText(text,baseX ,baseY ,paint);

        return bitmap;
    }



}
